package cz.myslivec.knihovna.action;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.log.Log;

import cz.myslivec.knihovna.dao.AuthorDaoImpl;
import cz.myslivec.knihovna.dao.BookCopyDaoImpl;
import cz.myslivec.knihovna.dao.CategoryDaoImpl;
import cz.myslivec.knihovna.dao.PublisherDaoImpl;
import cz.myslivec.knihovna.dao.TitleDaoImpl;
import cz.myslivec.knihovna.entity.Author;
import cz.myslivec.knihovna.entity.BookCopy;
import cz.myslivec.knihovna.entity.Category;
import cz.myslivec.knihovna.entity.Publisher;
import cz.myslivec.knihovna.entity.Title;

@Name("test01")
@AutoCreate
public class Test01 implements Serializable {
	private static final long serialVersionUID = 5195522566964891306L;
	
	@Logger
	private Log log;

	@In private AuthorDaoImpl authorDaoImpl;
	@In private BookCopyDaoImpl bookCopyDaoImpl;
	@In private CategoryDaoImpl categoryDaoImpl;
	@In private PublisherDaoImpl publisherDaoImpl;
	@In private TitleDaoImpl titleDaoImpl;
	
	public void test001() {
		createDB01();
		printDB();
	}
	
	private void createDB01() {
		// CATEGORY
		Category category = new Category();
		category.setCode("code");
		category.setName("cat01");
		category.setDescription("cat01 description");
		categoryDaoImpl.create(category);
		
		// PUBLISHER
		Publisher publisher = new Publisher();
		publisher.setName("publisher01");
		publisher.setAddress("addres of publisher01");
		publisherDaoImpl.create(publisher);
		
		// AUTHOR
		Author author = new Author();
		author.setName("George");
		author.setNote("note about Author1");
		author.setSurname("Author1");
		author.setTitle("mr.");
		author.setTranslator(false);
		author.setWriter(true);
		authorDaoImpl.create(author);
		
		// TRANSLATOR
		Author translator = new Author();
		translator.setName("John");
		translator.setNote("note about Translator1");
		translator.setSurname("Translator1");
		translator.setTitle("mr.");
		translator.setTranslator(true);
		translator.setWriter(false);
		authorDaoImpl.create(translator);
		
		// TITLE
		Title title = new Title();
		title.setAuthors(Arrays.asList(author));
		title.setTranslators(Arrays.asList(translator));
		title.setCategory(category);
		title.setPublisher(publisher);
		title.setBookSerialNmb(123);
		title.setIsbn("xxx");
		title.setLocation("b23");
		title.setNote("note");
		title.setTitle("The Big Book");
		title.setSubtitle("blabala");
		title.setYearPublication(2011);
		titleDaoImpl.create(title);
		
		// BOOK COPY
		BookCopy bookCopy = new BookCopy();
		bookCopy.setTitle(title);
		bookCopy.setBookCode("yyy");
		bookCopy.setNote("note");
		bookCopyDaoImpl.create(bookCopy);
	}
	
	private void printDB() {
		// CATEGORY
		log.info("Categories:  ");
		List<Category> categories = categoryDaoImpl.list();
		for(Category category : categories) {
			log.info("	#0", category);
		}
		log.info(" ");
		
		// PUBLISHER
		log.info("Publishers:  ");
		List<Publisher> publishers = publisherDaoImpl.list();
		for(Publisher publisher : publishers) {
			log.info("	#0", publisher);
		}
		log.info(" ");
		
		// AUTHOR
		log.info("Authors / translators:  ");
		List<Author> authors = authorDaoImpl.list();
		for(Author author : authors) {
			log.info("	#0", author);
		}
		log.info(" ");
		
		// TITLE
		log.info("Titles:  ");
		List<Title> titles = titleDaoImpl.list();
		for(Title title : titles) {
			log.info("	#0", title);
		}
		log.info(" ");
		
//		// BOOK COPY
//		log.info("Book copies:  ");
//		List<BookCopy> BookCopies = bookCopyDaoImpl.list();
//		for(Category category : categories) {
//			log.info("	#0", category);
//		}
//		log.info(" ");
	}
}
